package com.xtwy.controller;


import com.xtwy.entity.Poem;
import com.xtwy.service.PoemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 控制层
 */

@Controller
public class WebController {
    @Autowired
    private PoemServiceImpl poemService;

    /**
     * 添加数据
     * @return
     */
    @RequestMapping("/")
    public String index(){
//        List<Poem> poems = new ArrayList<>();
//        poems.add(new Poem(4,"湘春夜月·近清明","近清明,翠禽枝上消魂,可惜一片清歌，都付与黄昏。欲共柳花低诉，怕柳花轻薄，不解伤春。念楚乡旅宿，柔情别绪，谁与温存。"));
//        poems.add(new Poem(5,"卜算子·不是爱风尘","不是爱风尘，似被前缘误。花落花开自有时，总赖东君主。\n" +
//                "去也终须去，住也如何住！若得山花插满头，莫问奴归处"));
//        poems.add(new Poem(6,"御街行·秋日怀旧","纷纷坠叶飘香砌。夜寂静，寒声碎。真珠帘卷玉楼空，天淡银河垂地。年年今夜，月华如练，长是人千里。"));
//
//        for(int i=0;i<poems.size();i++){
//            //添加内容
//            poemService.save(poems.get(i));
//        }
        return "/index";
    }

    /**
     * 分页查询所有
     * @param pageIndex
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/contentList")
    public String contentList(
                       @RequestParam(value="pageIndex",required=false,defaultValue="0") int pageIndex,
                       @RequestParam(value="pageSize",required=false,defaultValue="10") int pageSize,
    Model model) {
        Pageable pageable = PageRequest.of(pageIndex,pageSize);
        Page<Poem> poems = poemService.findAll(pageable);
        List<Poem> poems1 = poems.getContent();
        model.addAttribute("poems",poems);
        return "/index";
    }

    /**
     * 按内容查询
     * @param content
     * @param pageIndex
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/pageList")
    public String pageList(@RequestParam(value="content",required=false,defaultValue="香") String content,
                         @RequestParam(value="pageIndex",required=false,defaultValue="0") int pageIndex,
                         @RequestParam(value="pageSize",required=false,defaultValue="10") int pageSize,
                         Model model) {
        Pageable pageable =PageRequest.of(pageIndex,pageSize);
        Page<Poem> poems = poemService.search(content,pageable);
        List<Poem> list = poems.getContent();
        model.addAttribute("poems",list);
        return "/pageList";
    }

    /**
     *  搜索
     * @param content
     * @param pageIndex
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/search")
    public String search(String content, @RequestParam(value="pageIndex",required=false,defaultValue="0") int pageIndex,
                         @RequestParam(value="pageSize",required=false,defaultValue="10") int pageSize,Model model) {
                //分页
                Pageable pageable = PageRequest.of(pageIndex,pageSize);
                //添加分页
                Page<Poem> poems = poemService.search(content,pageable);
                //获取内容
                List<Poem> list = poems.getContent();
                model.addAttribute("poems",list);
                return "/list";
    }
}
